{
 "metadata": {
  "language_info": {
   "name": "python",
   "version": "3.7.6",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "orig_nbformat": 4,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.6 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "af997f310b38b8fff913e3035512343e167e2ca0aa226bb6d1eaa902db614b07"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "import numpy as np"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a = np.array([10,9,6,7])\r\n",
    "b = np.arange(10)\r\n",
    "print(a)\r\n",
    "print(b)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[10  9  6  7]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# np.random.randint创建一个N行N列的随机数数组\r\n",
    "d = np.random.randint(0,9,size=(4,4))\r\n",
    "d"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[2, 8, 0, 0],\n",
       "       [5, 1, 7, 7],\n",
       "       [5, 6, 0, 8],\n",
       "       [7, 7, 6, 3]])"
      ]
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a = np.array([\r\n",
    "    [1,2,3,4],\r\n",
    "    [2,3,4,5]\r\n",
    "])\r\n",
    "print(a)\r\n",
    "# reshape修改数组维度 不会改变原数组形状\r\n",
    "b = a.reshape((8,))\r\n",
    "print(b)\r\n",
    "print(b.size)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[[1 2 3 4]\n",
      " [2 3 4 5]]\n",
      "[1 2 3 4 2 3 4 5]\n",
      "8\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 二维数组的索引，切片\r\n",
    "a = np.random.randint(0,10,size=(4,6))\r\n",
    "# 也是通过中括号来索引和切片，在中括号中，使用逗号进行分割，逗号前面是行，逗号后面是列，如果多维数组中只有一个值那个这个值就是行\r\n",
    "a"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[6, 8, 6, 7, 4, 6],\n",
       "       [7, 8, 4, 1, 8, 5],\n",
       "       [6, 3, 5, 8, 1, 9],\n",
       "       [8, 1, 0, 6, 5, 3]])"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a[0]\r\n",
    "a[1:3] # 获取第一行，第二行\r\n",
    "a[[0,2,3]] # 获取0,2,3行 获取不连续的行\r\n",
    "a[1,1] # 第一行第一列\r\n",
    "a[:2,:3] # 0,1行 0,1,2列\r\n",
    "a[1:3,4:6] # 1 2行  4 5列\r\n",
    "a[:,1]  # 第一列"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([8, 8, 3, 1])"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 布尔索引\r\n",
    "a[a<5]\r\n",
    "a[(a < 5) & (a > 2)]"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([4, 4, 3, 3])"
      ]
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 数组与数之间的运算\r\n",
    "a = np.random.randint(0,5,size=(3,5))\r\n",
    "a"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[4, 1, 0, 0, 2],\n",
       "       [2, 1, 2, 2, 1],\n",
       "       [4, 0, 3, 3, 2]])"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a*2"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[8, 2, 0, 0, 4],\n",
       "       [4, 2, 4, 4, 2],\n",
       "       [8, 0, 6, 6, 4]])"
      ]
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 数组与数组的运算\r\n",
    "a1 = np.random.randint(0,5,size=(3,5))\r\n",
    "a1"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[1, 0, 4, 0, 1],\n",
       "       [3, 1, 3, 1, 1],\n",
       "       [4, 3, 3, 4, 0]])"
      ]
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a1 + a"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[5, 1, 4, 0, 3],\n",
       "       [5, 2, 5, 3, 2],\n",
       "       [8, 3, 6, 7, 2]])"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a2 = np.random.randint(0,5,size=(3,1))\r\n",
    "a2"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [4],\n",
       "       [2]])"
      ]
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# shape不同不可以进行运算 但相同行的可以与只有一列的相加，这一列与另一个数组中每一列进行相加\r\n",
    "# 称为数组的广播机制\r\n",
    "a2 + a1"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[2, 1, 5, 1, 2],\n",
       "       [7, 5, 7, 5, 5],\n",
       "       [6, 5, 5, 6, 2]])"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 数组的转置\r\n",
    "a = np.random.randint(0,10,size=(3,4))\r\n",
    "a"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[7, 9, 5, 2],\n",
       "       [9, 5, 5, 3],\n",
       "       [7, 6, 7, 0]])"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a.T"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[7, 9, 7],\n",
       "       [9, 5, 6],\n",
       "       [5, 5, 7],\n",
       "       [2, 3, 0]])"
      ]
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# dot 线性代数矩阵相乘\r\n",
    "a.dot(a.T)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[159, 139, 138],\n",
       "       [139, 140, 128],\n",
       "       [138, 128, 134]])"
      ]
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 不拷贝  只是简单的赋值\r\n",
    "b = a\r\n",
    "b"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[7, 9, 5, 2],\n",
       "       [9, 5, 5, 3],\n",
       "       [7, 6, 7, 0]])"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 浅拷贝\r\n",
    "b = a.view()\r\n",
    "b"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[7, 9, 5, 2],\n",
       "       [9, 5, 5, 3],\n",
       "       [7, 6, 7, 0]])"
      ]
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 深拷贝\r\n",
    "b = a.copy()\r\n",
    "b\r\n",
    "print(b is a)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "False\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 读取csv文件\r\n",
    "# with open(\"aaa.csv\",\"r\") as f:\r\n",
    "#    reader = csv.reader(f)\r\n",
    "#     for x in reader:\r\n",
    "#         print(x)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a = np.random.randint(1,10,size=(3,5)).astype(np.float)\r\n",
    "a"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[1., 9., 5., 3., 2.],\n",
       "       [6., 6., 2., 9., 1.],\n",
       "       [7., 9., 7., 4., 9.]])"
      ]
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a[1,2] = np.NaN\r\n",
    "a[2,3] = np.NaN\r\n",
    "a"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[ 1.,  9.,  5.,  3.,  2.],\n",
       "       [ 6.,  6., nan,  9.,  1.],\n",
       "       [ 7.,  9.,  7., nan,  9.]])"
      ]
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 删除nan后 会变成一位数组\r\n",
    "a[np.isnan(a)]\r\n",
    "a[~np.isnan(a)]"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([1., 9., 5., 3., 2., 6., 6., 9., 1., 7., 9., 7., 9.])"
      ]
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 将nan全部替换成指定值\r\n",
    "a[np.isnan(a)] = 0\r\n",
    "a"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[1., 9., 5., 3., 2.],\n",
       "       [6., 6., 0., 9., 1.],\n",
       "       [7., 9., 7., 0., 9.]])"
      ]
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 删除nan所在行\r\n",
    "d = np.delete(a,np.where(np.isnan(a))[0],axis=0)\r\n",
    "d"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[1., 9., 5., 3., 2.],\n",
       "       [6., 6., 0., 9., 1.],\n",
       "       [7., 9., 7., 0., 9.]])"
      ]
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "np.sort(d,axis=0)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[1., 6., 0., 0., 1.],\n",
       "       [6., 9., 5., 3., 2.],\n",
       "       [7., 9., 7., 9., 9.]])"
      ]
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "-np.sort(-d)\r\n",
    "# daox排序"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[9., 5., 3., 2., 1.],\n",
       "       [9., 6., 6., 1., 0.],\n",
       "       [9., 9., 7., 7., 0.]])"
      ]
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "# d"
   ],
   "outputs": [],
   "metadata": {}
  }
 ]
}